package web.servlet;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import pojo.Course;
import service.iml.ServiceDao;
import service.iml.ServiceDaoImpl;
import utils.DateUtils;
import utils.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courseSalesInfo")
public class CourseSalesInfoServlet extends HttpServlet {
    ServiceDao sd = new ServiceDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            Course course = new Course();
            Map<String,Object> map = new HashMap<>();
            //创建磁盘工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //文件上传核心对象
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            //解析request对象
            List<FileItem> list = fileUpload.parseRequest(req);
            for (FileItem item : list) {
                boolean formField = item.isFormField();
                if (formField){
                    //普通表单项
                    String fieldName = item.getFieldName();
                    String value = item.getString("utf-8");
                    map.put(fieldName,value);
                }else {
                    //上传文件项
                    String fileName = item.getName();
                    String uuid = UUIDUtils.getUUID();
                    fileName = uuid + "_" + fileName;
                    InputStream in = item.getInputStream();
                    String realPath = this.getServletContext().getRealPath("/");
                    String webappsPath = realPath.substring(0, realPath.indexOf("task_work"));
                    FileOutputStream out = new FileOutputStream(webappsPath + "/upload/" +fileName);
                    IOUtils.copy(in,out);
                    out.close();
                    in.close();
                    map.put("course_img_url","/upload/" + fileName);
                }
            }
            String formart = DateUtils.getDateFormart();
            BeanUtils.populate(course,map);
            if (map.get("id") != null){
                //修改
                course.setCreate_time(formart);
                course.setUpdate_time(formart);
                String result = sd.updateCourse(course);
                resp.getWriter().print(result);
            }else {
                //新建
                course.setCreate_time(formart);
                course.setUpdate_time(formart);
                course.setStatus(1);
                String result = sd.courseSalesInfo(course);
                resp.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
